﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using YJKCodeBase.Geometry;
using YJKCodeBase.Math;
using YJKCodeBase.TopologySuite;

namespace YJKRevitCode.RevitTopologySuite
{
	public class RevitPolygonBooleanOperater : IRevitGeometryBoolean
	{
		public bool TryGetBooleanResult(RevitOperationDataBean inputOne, RevitOperationDataBean inputTwo, BooleanOperatorKindEnum inputKind, out List<RevitOperationDataBean> creatResult)
		{
			creatResult = null;
			if (!(inputOne is RevitPolygonDataBean) || !(inputTwo is RevitPolygonDataBean))
			{
				return false;
			}
			List<RevitPolygonDataBean> list = null;
			if (!this.TryGetBooleanResult(inputOne as RevitPolygonDataBean, inputTwo as RevitPolygonDataBean, inputKind, out list))
			{
				return false;
			}
			if (list == null)
			{
				return false;
			}
			creatResult = list.Cast<RevitOperationDataBean>().ToList<RevitOperationDataBean>();
			return true;
		}

		public bool TryGetBooleanResult(RevitPolygonDataBean inputOne, RevitPolygonDataBean inputTwo, BooleanOperatorKindEnum inputKind, out List<RevitPolygonDataBean> creatResult)
		{
			HYPolygon inputPolygonOne = null;
			HYPolygon inputPolygonTwo = null;
			creatResult = null;
			List<Arc> lstPotentialArc = null;
			if (inputOne == null || inputTwo == null)
			{
				return false;
			}
			if (!GeometryTransformer.TryMakeHYPolygon(inputOne, out inputPolygonOne) || !GeometryTransformer.TryMakeHYPolygon(inputTwo, out inputPolygonTwo))
			{
				return false;
			}
			if (!MathUtility.IsEqual(inputOne.NowZ, inputTwo.NowZ))
			{
				return false;
			}
			this.FindPotentialArc(inputOne, ref lstPotentialArc);
			this.FindPotentialArc(inputTwo, ref lstPotentialArc);
			List<HYPolygon> list = null;
			HYPolygonBooleanOperater hypolygonBooleanOperater = new HYPolygonBooleanOperater();
			if (!hypolygonBooleanOperater.TryGetOperateResult(inputPolygonOne, inputPolygonTwo, inputKind, out list))
			{
				return false;
			}
			creatResult = new List<RevitPolygonDataBean>();
			RevitPolygonDataBean revitPolygonDataBean = null;
			if (list != null)
			{
				foreach (HYPolygon inputPolygon in list)
				{
					if (!GeometryTransformer.TryMakeRevitPolygonData(inputPolygon, inputOne.NowZ, out revitPolygonDataBean, lstPotentialArc))
					{
						return false;
					}
					if (revitPolygonDataBean != null)
					{
						creatResult.Add(revitPolygonDataBean);
					}
				}
				return true;
			}
			return true;
		}

		private void FindPotentialArc(RevitPolygonDataBean input, ref List<Arc> useLstArc)
		{
			List<Arc> list = new List<Arc>();
			foreach (Curve curve in input.LstRevitCurve)
			{
				if (curve is Arc)
				{
					list.Add(curve as Arc);
				}
			}
			if (input.LstlstHolesCurves != null)
			{
				foreach (List<Curve> list2 in input.LstlstHolesCurves)
				{
					foreach (Curve curve2 in list2)
					{
						if (curve2 is Arc)
						{
							list.Add(curve2 as Arc);
						}
					}
				}
			}
			if (list.Count == 0)
			{
				return;
			}
			if (useLstArc == null)
			{
				useLstArc = new List<Arc>();
			}
			useLstArc.AddRange(list);
		}
	}
}
